#include "CustomCursor.h"
#include "Screen.h"
#include "Factory.h"
#include "TextureManager.h"
#include <gl\glut.h>

CustomCursor::CustomCursor()
{
	SDL_ShowCursor(SDL_DISABLE);
	m_screenSize = TheScreen::Instance()->GetScreenSize();
	TheEventPoller::Instance()->AddHandler(this);
	m_texPath = "Textures/CustomCursor.png";
}

CustomCursor::~CustomCursor()
{
	TheEventPoller::Instance()->RemoveHandler(this);
}

void CustomCursor::Draw()
{
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

	TheTextureManager::Instance()->DrawTexture(m_texPath);

	glBegin(GL_QUADS);
		//counter clockwise winding 
		glTexCoord2f(0.0f, 1.0f); //coord for points
		glVertex3f((m_pos.X - 1.0f), (m_pos.Y - 1.0f), 0); //UVs

		glTexCoord2f(1.0f, 1.0f); //coord for points
		glVertex3f((m_pos.X - 1.0f) + m_dimensions.X, (m_pos.Y - 1.0f), 0); //UVs

		glTexCoord2f(1.0f, 0.0f); //coord for points
		glVertex3f((m_pos.X - 1.0f) + m_dimensions.X, 
			(m_pos.Y - 1.0f) + m_dimensions.Y, 0); //UVs

		glTexCoord2f(0.0f, 0.0f); //coord for points
		glVertex3f((m_pos.X - 1.0f), (m_pos.Y - 1.0f) + m_dimensions.Y, 0); //UVs
		glEnd();
	glDisable(GL_BLEND);
}

bool CustomCursor::Load(File* const f)
{
	return true;
}

void CustomCursor::OnMouseMotionEvent(const SDL_MouseMotionEvent& mm)
{
	SDL_GetMouseState(&m_SDLmouseCoord.X, &m_SDLmouseCoord.Y);
	CoordConvert();

	// ... - - m_dimensions.Y, to move cursor down so 
	//clicking from top left not bottom left
	m_pos = Vec2f(m_OGLmouseCoord.X, m_OGLmouseCoord.Y - m_dimensions.Y);
}

const char* CustomCursor::GetTypeName()
{
	return "CustomCursor";
}

GUIObject* CreateCustomCursor()
{
	return new CustomCursor;
}

static bool cc = TheGUIObjectFactory::Instance()->AddToMap("CustomCursor", CreateCustomCursor);